﻿Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient

Public Class TurnosDB

    Public Shared Function GetTurnos() As DataSet
        Dim cmd As New SqlCommand("spGetTurnos", DB.Conexion)
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet

        cmd.CommandType = CommandType.StoredProcedure
        da.SelectCommand = cmd
        da.Fill(ds, "Turnos")
        Return ds
    End Function
    Public Shared Sub AsignarTurno(ByVal id_empleado As Integer, ByVal id_turno As Integer, ByVal fecha As Date)
        Dim cmd As New SqlCommand("spAsignarTurno", DB.Conexion)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@id_empleado", id_empleado)
        cmd.Parameters.AddWithValue("@id_turno", id_turno)
        cmd.Parameters.AddWithValue("@fecha", fecha)
        cmd.Parameters.Add("@Error", SqlDbType.Int).Direction = ParameterDirection.Output
        cmd.Parameters.Add("@ErrorDesc", SqlDbType.VarChar, 200).Direction = ParameterDirection.Output
        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
        If cmd.Parameters("@error").Value <> 0 Then
            Throw New ApplicationException(cmd.Parameters("@ErrorDesc").Value.ToString())
        End If
    End Sub

    Public Shared Sub EliminarTurnoEmpleado(ByVal id_empleado As Integer, ByVal id_turno As Integer, ByVal semana As Date)
        Dim cmd As New SqlCommand("spEliminarTurnoEmpleado", DB.Conexion)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@id_empleado", id_empleado)
        cmd.Parameters.AddWithValue("@id_turno", id_turno)
        cmd.Parameters.AddWithValue("@semana", semana)
        cmd.Parameters.Add("@Error", SqlDbType.Int).Direction = ParameterDirection.Output
        cmd.Parameters.Add("@ErrorDesc", SqlDbType.VarChar, 200).Direction = ParameterDirection.Output
        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
        If cmd.Parameters("@error").Value <> 0 Then
            Throw New ApplicationException(cmd.Parameters("@ErrorDesc").Value.ToString())
        End If
    End Sub

    Public Shared Function GetTurnosEmpleado(ByVal id_empleado As Integer) As DataSet
        Dim cmd As New SqlCommand("spGetTurnosEmpleado", DB.Conexion)
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet

        If id_empleado = 0 Then
            cmd.Parameters.AddWithValue("@id_empleado", DBNull.Value)
        Else
            cmd.Parameters.AddWithValue("@id_empleado", id_empleado)
        End If


        cmd.CommandType = CommandType.StoredProcedure
        da.SelectCommand = cmd
        da.Fill(ds, "TurnosEmpleado")
        Return ds
    End Function

    Public Shared Function GetTurnosByMes(ByVal nro_mes As Integer) As DataSet
        Dim cmd As New SqlCommand("spGetTurnosEmpleadoByMes", DB.Conexion)
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet

        If nro_mes = 0 Then
            cmd.Parameters.AddWithValue("@nro_mes", DBNull.Value)
        Else
            cmd.Parameters.AddWithValue("@nro_mes", nro_mes)
        End If


        cmd.CommandType = CommandType.StoredProcedure
        da.SelectCommand = cmd
        da.Fill(ds, "TurnosEmpleado")
        Return ds
    End Function
End Class
